package com.wzc.user.controller;

import com.wzc.basic.exception.BusinessException;
import com.wzc.basic.util.AjaxResult;
import com.wzc.user.dto.LoginDto;
import com.wzc.user.dto.WxDto;
import com.wzc.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;


    /**
     * 登录
     * @param dto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> map = loginService.account(dto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常！");
        }
    }

    /**
     * 微信扫码登录
     * @param code
     * @return
     */
    @GetMapping("/weChat/{code}")
    public AjaxResult weChat(@PathVariable("code") String code){
        try {

            return loginService.weChat(code);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常！");
        }
    }


    /**
     * 微信绑定
     * @param dto
     * @return
     */
    @PostMapping("/weChat/binder")
    public AjaxResult binder(@RequestBody WxDto dto){
        try {

            return loginService.binder(dto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常！");
        }
    }

}
